define(['jquery', './util/WebApi', 'jcarousel', 'slider', 'leanModal', 'validate'], function($, WebApi){
	
	/*main page*/
	function initCarousel(){
		var jcarousel = $('.jcarousel');
       
        jcarousel.jcarousel({
        	wrap: 'circular'
		});

        $('.jcarousel-control-prev').jcarouselControl({
           target: '-=1'
        });

        $('.jcarousel-control-next').jcarouselControl({
           target: '+=1'
        });
	}
	
	/*main page*/
	function initSlider(){
		var CMP_NAME = 'unslider';
		var $slider = $('.slider');
		
		$slider.unslider({
			speed: 1500,
			dots: true,
			fluid: false,
			complete: function(context){
				var inst = $(context).data(CMP_NAME);
				var currIdx = inst.current;
				var currentItem = inst.items[currIdx];
				$(inst.el).children('.panel').html($(currentItem).find('.panel').html());
			}
		});
		
		var inst = $slider.data(CMP_NAME);
		var currentItem = inst.items[inst.current];
		$(inst.el).append($(currentItem).find('.panel').clone());
	}
	
	$(function(){
		initCarousel();
		initSlider();
		
		$('#newOrderModal form').on('submit', function(e){
			var form = e.target;
			var $form = $(form);
			
			var success = function() {
				$('#confirmSentModal').trigger('show');
			};
			
			var error = function() {
				$('#serverErrorModal').trigger('show');
			};
			
			if($form.valid()){
				WebApi.submitOrderForm({
					name : form.name.value,
					phone : form.phone.value,
					message : form.message.value,
					reason : form.reason.value
				}, success, error);
			}

			e.preventDefault();
			return false;
		});
	});

});